package toanstt.com.MatchUp;

import java.io.IOException;
import java.util.Random;








import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.util.Log;
import android.view.Display;
import android.widget.Toast;

public class T 
{
	static public int i;
	static public int DISPLAY_WIDTH;
	static public int DISPLAY_HEIGHT;
	static public Display display;
	static public float U1;
	static public float U2;
	static public Paint paint;
	static public Paint mainpaint;
	static public Paint paint_alpha;
	static public long CURRENT_TIME=0;
	static public Random random;
	static public int SCREEN = 2;
	static public boolean onnotice = false;
	static public long DELAY_TIME=0;
	static public long PAST_TIME=0;
	static public Matrix mat,mat_end;
	static public float scale1,scale2,scale;
	
	static public int CELL_WIDTH;
	static public int CELL_HWIDTH;
	static public int KEYS_WIDTH;
	static public int BUT_WIDTH;
	static public int BUT_HEIGHT;
	static public int GAME_HEIGHT;
	static public int GAME_WIDTH;
	static public int GAME_MINX;
	static public int GAME_MINY;
	static public int GAME_COL;
	static public int GAME_ROW;
	static public boolean IS_BUSY=false;
	
	static public int STATUS_MINX;
	static public int PLAYER_I1=0;
	static public int PLAYER_I2=0;
	static public int STATUS_MINY;
	static public int STATUS_MAXY;
	static public int STATUS_HEIGHT;
	static public int STATUS_WIDTH;
	static public int GAME_I1;
	static public int GAME_I2;
	static public boolean ISHORIZONTAL = true;
	static public int PANEL_MINX;
	static public int PANEL_MINY;
	static public int ALPHA =0;
	static public int ALPHA_COUNT_MAX =20;
	static public int BANNER_HEIGHT;
	static public boolean THEME_WHITE = true;
	static public int[] TIME_BOUNS;
	//bitmap
	static public int[][] GAME_DATA;
	static public TBitmap TBITMAP_PLAYER;
	static public TBitmap2 TBITMAP_BANNER;
	static public TBitmap2 TBITMAP_KEYS;
	static public TBitmap2 TBITMAP_HOWTOPLAY;
	
	static public TBitmap2 TBITMAP_NUMBERS;
	
	static public TBitmap2 TBITMAP_BUTTON_MENU;	
	static public TBitmap2 TBITMAP_BUTTON_MENU_WHITE;
	static public TBitmap2 TBITMAP_THEME_BLACK;
	static public TBitmap2 TBITMAP_THEME_WHITE;
	static public TBitmap2 TBITMAP_THUMBS;
	static public TBitmap2 TBITMAP_BANER;	
	static public TBitmap2 TBITMAP_STARS;
	static public int SOUND =5;
	static public BitmapFont bitmapfont;
	static public BitmapFont bitmapfont1;
	static public BitmapFont bitmapfont2;
	static public TBitmap2 BITMAP_BLASH;
	public static Notice notice;
	public static int PAST_TIME2;
	static public  Sound1 soundtheme;
	public static Sound2 sounds;
	public static int SCREEN_TEMP=-1;
	public static int GAMEPLAYMODE=0;
	
	static public void Load(MainActivity parrent)
	{
		if(T.ISHORIZONTAL)
		{
			PAST_TIME =0;
			CELL_WIDTH = (int)(U2*30/9);
			CELL_HWIDTH =CELL_WIDTH/2; 
			GAME_HEIGHT = GAME_WIDTH =CELL_WIDTH*9; 
			KEYS_WIDTH = DISPLAY_WIDTH - DISPLAY_HEIGHT;
			BUT_HEIGHT = BUT_WIDTH = (KEYS_WIDTH)/4;
			PANEL_MINX = T.DISPLAY_HEIGHT;
			PANEL_MINY = T.DISPLAY_HEIGHT - (T.DISPLAY_WIDTH - T.DISPLAY_HEIGHT) - 50;
			/*if(T.DISPLAY_WIDTH  < 400)
			{
				T.BUT_WIDTH = T.BUT_HEIGHT = (T.DISPLAY_HEIGHT  - 50)/6;
				CELL_WIDTH = (T.DISPLAY_HEIGHT  - 50)/9;
				CELL_HWIDTH =CELL_WIDTH/2; 
				GAME_HEIGHT = GAME_WIDTH =CELL_WIDTH*9; 
				PANEL_MINX = GAME_WIDTH;
				PANEL_MINY =(int)(2*2.4f*T.U2) ;
			}*/
			
			T.paint = new Paint();
			T.paint_alpha = new Paint();
			T.random = new Random();
			GAME_MINX = GAME_MINY = 0;
		}
		else
		{
			GAME_MINX =0;
			GAME_MINY = (int)(3*T.U2);
			PAST_TIME =0;
			CELL_WIDTH = (int)(U1*50/9);
			CELL_HWIDTH =CELL_WIDTH/2; 
			GAME_HEIGHT = GAME_WIDTH =CELL_WIDTH*9  ; 
			KEYS_WIDTH = DISPLAY_WIDTH;
			BUT_HEIGHT = BUT_WIDTH = (KEYS_WIDTH)/8;
			
			PANEL_MINX = 0;
			PANEL_MINY = GAME_WIDTH + GAME_MINY + (int)(2*U2);
			T.paint = new Paint();
			T.paint_alpha = new Paint();
			T.random = new Random();
		}
		sounds = new Sound2();
		 soundtheme = new Sound1();
		LoadBitmap();
		
		TIME_BOUNS = new int[4];
		TIME_BOUNS[0] = 2;
		TIME_BOUNS[1] = 3;
		TIME_BOUNS[2] = 4;
		TIME_BOUNS[3] = 6;

	}
	
	

	static public void Init_loading(MainActivity parrent)
	{
		LoadInfo();
		if(T.ISHORIZONTAL == false)
		{
			MainActivity.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
		display = parrent.getWindowManager().getDefaultDisplay();
        DISPLAY_WIDTH = display.getWidth();
        DISPLAY_HEIGHT= display.getHeight();
        U1= DISPLAY_WIDTH*1.0f/50;
        U2= DISPLAY_HEIGHT*1.0f/30; 
        mat = new Matrix();
        mat_end= new Matrix();
        scale1 = DISPLAY_WIDTH*1.0f/800;
        scale2 = DISPLAY_WIDTH*1.0f/480;
        scale =scale1;
        if(scale2 < scale)
        	scale =scale2;
        mat_end.setScale(scale, scale);
        random = new Random();
        BANNER_HEIGHT =0;
        mainpaint = new Paint();
        mainpaint.setAntiAlias(true);
        mainpaint.setFilterBitmap(true);
        mainpaint.setDither(true);
        //notice = new Notice();
        T.Show("display width height" + T.DISPLAY_WIDTH + " " + T.DISPLAY_HEIGHT);
        BITMAP_BLASH = new TBitmap2("blast.png", 1, 1, T.DISPLAY_WIDTH);
        paint_alpha = new Paint();
	}
	public static void LoadBitmap()
	{
		
		if(T.ISHORIZONTAL)
		{
			bitmapfont1 = new BitmapFont("font/white_36.spr", false,T.U2/36);
			bitmapfont2 = new BitmapFont("bitmapfont/bitmapfont2.spr", false,2.4f*T.U2/100);
			
			
			TBITMAP_BUTTON_MENU_WHITE = new TBitmap2("key/menu_button_white.png", 22, 1, (int)(T.DISPLAY_WIDTH/(1.5)));
			
			TBITMAP_BUTTON_MENU =TBITMAP_BUTTON_MENU_WHITE; 
			
		
			if(T.THEME_WHITE)
			{
		
				bitmapfont = bitmapfont2;
			}
			else
			{
		
				bitmapfont = bitmapfont1;
			}
			
			TBITMAP_THEME_BLACK = new TBitmap2("theme/theme1.png", 1, 1, T.DISPLAY_WIDTH);
			TBITMAP_THEME_WHITE = new TBitmap2("theme/theme2.png", 1, 1, T.DISPLAY_WIDTH,T.DISPLAY_HEIGHT);
			TBITMAP_THUMBS = new TBitmap2("key/thumb.png", 1, 5, T.DISPLAY_WIDTH/5);
			//gamestatus
			TBITMAP_BANER = new TBitmap2("key/banner.png", 1, 1, (int)(T.DISPLAY_WIDTH*0.9));
			TBITMAP_STARS = new TBitmap2("key/stars.png", 3, 1, (int)(T.DISPLAY_WIDTH/1.5));
			
			
			TBITMAP_BANNER = new TBitmap2("banner.png", 1, 1, T.DISPLAY_HEIGHT,true );
			TBITMAP_HOWTOPLAY = new TBitmap2("howtoplay.png", 1, 1, DISPLAY_WIDTH, DISPLAY_HEIGHT);
		}
		else
		{
			Matrix m = new Matrix();
			m.postRotate(90);
			bitmapfont1 = new BitmapFont("font/white_36.spr", false,T.U2/36);
			bitmapfont2 = new BitmapFont("bitmapfont/bitmapfont2.spr", false,2f*T.U2/100);
			
			TBITMAP_BUTTON_MENU_WHITE = new TBitmap2("key/menu_button_white.png", 22, 1, (int)(30*U1));
			 
			TBITMAP_BUTTON_MENU =TBITMAP_BUTTON_MENU_WHITE; 
			
			if(T.THEME_WHITE)
			{
			
				bitmapfont = bitmapfont2;
			}
			else
			{
			
				bitmapfont = bitmapfont1;
			}
			TBITMAP_THEME_BLACK = new TBitmap2("theme/theme1.jpg", 1, 1, T.DISPLAY_HEIGHT);
			TBITMAP_THEME_BLACK.bitmap[0] = Bitmap.createBitmap(TBITMAP_THEME_BLACK.bitmap[0], 0, 0,TBITMAP_THEME_BLACK.bitmap[0].getWidth(), TBITMAP_THEME_BLACK.bitmap[0].getHeight(), m, true);
			
			TBITMAP_THEME_WHITE = new TBitmap2("theme/theme2.jpg", 1, 1, T.DISPLAY_HEIGHT);
			TBITMAP_THEME_WHITE.bitmap[0] = Bitmap.createBitmap(TBITMAP_THEME_WHITE.bitmap[0], 0, 0,TBITMAP_THEME_WHITE.bitmap[0].getWidth(), TBITMAP_THEME_WHITE.bitmap[0].getHeight(), m, true);
			TBITMAP_THUMBS = new TBitmap2("key/thumbs.png", 5, 4, (int)(3*U2));
			//gamestatus
			//TBITMAP_BANER = new TBitmap2("key/banner.png", 1, 1, T.DISPLAY_WIDTH - T.GAME_WIDTH);
			TBITMAP_BANER = new TBitmap2("key/banner.png", 1, 1, T.DISPLAY_WIDTH);
			TBITMAP_STARS = new TBitmap2("key/stars.png", 3, 1, T.DISPLAY_WIDTH/3);
			
			
			TBITMAP_BANNER = new TBitmap2("banner.png", 1, 1, T.DISPLAY_HEIGHT,true );
		}
		
	}
	public static Bitmap loadImageFromAsset(String path) 
	{

		AssetManager am = MainActivity.activity.getAssets();


	    BitmapFactory.Options opt = new BitmapFactory.Options();

	    opt.inSampleSize = 1;
	    //opt.
	    Bitmap a=null;

	    opt.inPreferredConfig = Config.RGB_565;
	


	//    Rect rect = new Rect(1, 1, 1, 1);
		try {
			a= BitmapFactory.decodeStream(am.open(path),null,opt);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return a;
	}
	
	public static boolean Inside(int x,int y,int x1,int y1,int x2,int y2)
	{
		if(x == x1 && x1 == x2)
		{
			if(y1 <= y && y<=y2)
				return true;
			if(y1 >= y && y>=y2)
				return true;
		}
		if(y == y1 && y1 == y2)
		{
			if(x1 <= x && x<=x2)
				return true;
			if(x1 >= x && x>=x2)
				return true;
		}
		return false;
	}
	
	static public float Distance(float x,float y,float x2,float y2)
	{
		return (float) Math.sqrt(((x2-x)*(x2-x)+(y2-y)*(y2-y)));
	}
	static public float Distance(int x,int y,int x2,int y2)
	{
		return (float) Math.sqrt(((x2-x)*(x2-x)+(y2-y)*(y2-y)));
	}
	
	static public int Abs(int d)
	{
		if(d>0)
			return d;
		else
			return -d;
		
	}
	static public int Length(int x1,int y1,int x2,int y2)
	{
		if(x1 == x2)
			return Math.abs(y2-y1);
		if(y1 == y2)
			return Math.abs(x2-x1);
		Log.e("TTTTTTTTT","not a line");
		Log.d("TTTTTTTTT","not a line");
		return -1;
	}
	
	
	
	static public void Show(String a,String a2)
	{
		Log.d(a,a2);
	}
	static public void Show(String a)
	{
		Log.d("Testttt",a);
	}
	private static void LoadData() 
	{
		
		
		
		
	}
	public static void LoadInfo()
	{
		SharedPreferences settings = MainActivity.activity.getSharedPreferences("toanCARD1", 0);
		//STAGE= settings.getInt("STAGE",STAGE);
		GAME_DATA = new int[4][];
		for(int i =0;i<4;i++)
		{
			GAME_DATA[i] = new int[24];
			for(int j =0;j<24;j++)
				GAME_DATA[i][j] = 0;
		}
		String a;
		a = settings.getString("GAME_DATA", null);
		THEME_WHITE = settings.getBoolean("THEME_WHITE", true);
		ISHORIZONTAL = settings.getBoolean("ISHORIZONTAL", true);
		SOUND = settings.getInt("SOUND", 5);
		PLAYER_I1 = settings.getInt("PLAYER_I1", 0);
		PLAYER_I2 = settings.getInt("PLAYER_I2", 0);
		if(a == null)
			return;
		int index =0;
		for(int i =0;i<4;i++)
		{
			for(int j =0;j<24;j++)
			{
				GAME_DATA[i][j] = Integer.parseInt( (a.charAt(index) + ""));
				index++;
			}
		}
	}
	static public void SaveInfo()
	{
		SharedPreferences settings = MainActivity.activity.getSharedPreferences("toanCARD1", 0);
		SharedPreferences.Editor editor = settings.edit();
		//editor.putInt("STAGE",STAGE);
		String a="";
		for(int i =0;i<4;i++)
		{
			for(int j =0;j<24;j++)
			{
				a+= GAME_DATA[i][j];
			}
		}

		editor.putString("GAME_DATA", a);
		editor.putBoolean("THEME_WHITE",THEME_WHITE);
		editor.putBoolean("ISHORIZONTAL",ISHORIZONTAL);
		editor.putInt("SOUND",SOUND);
		editor.putInt("PLAYER_I1",PLAYER_I1);
		editor.putInt("PLAYER_I2",PLAYER_I2);
		editor.commit();
	}
	static public void TOAST(CharSequence text)
	{
		
		
		

		Toast toast = Toast.makeText(MainActivity.context,text, Toast.LENGTH_SHORT);
		
		//Toast toast = Toast.makeText(MainActivity.context, "pl", Toast.LENGTH_SHORT);
		toast.show();
	}
}
